package com.firesrc.web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;

/**
 * Servlet implementation class ViewAllKeyword
 */
public class ViewUnSearchedKeyword extends SuperServlet {
  private static final long serialVersionUID = 1L;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public ViewUnSearchedKeyword() {
    super();
    // TODO Auto-generated constructor stub
  }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=gb2312");
    long count = getUnSearchKeywordCount();
    List<String> list = getUnSearchMailList();
    request.setAttribute("list", list);
    request.setAttribute("count", count);
    request.getRequestDispatcher("unSearchedKeyword.jsp").forward(request, response);
  }

  private long getUnSearchKeywordCount() {
    DbThreadSession dbThreadSession = new DbThreadSession(cp);
    String sql = "select count(distinct name)  from KEYWORD   where state='0'";
    DbOomQuery q = new DbOomQuery(sql);
    long count = q.executeCount();
    dbThreadSession.closeSession();
    return count;
  }

  public List<String> getUnSearchMailList() {
    DbThreadSession dbThreadSession = new DbThreadSession(cp);

    String sql = "select top 20 name  from KEYWORD   where state='0'";

    DbOomQuery q = new DbOomQuery(sql);
    ResultSet execute = q.execute();
    List<String> list = new ArrayList<String>();
    try {
      while (execute.next()) {
        list.add(execute.getString(1));
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    dbThreadSession.closeSession();
    return list;
  }

}
